package com.sopt.friends.sub;

import java.util.ArrayList;

import com.sopt.friends.ChatActivity;
import com.sopt.friends.R;
import com.sopt.friends.db.FriendsDBHelper;
import com.sopt.friends.db.Constants.FriendsDB;
import com.sopt.friends.util.FriendsGroupManager;
import com.sopt.friends.util.FriendsSms;
import com.sopt.friends.vo.FriendsBasicMember;
import com.sopt.friends.vo.FriendsCommonItem;
import com.sopt.view.GroupMemberAdapter;
import com.sopt.view.GroupMemberEditAdapter;
import com.sopt.view.HomeMemberAdapter;
import com.sopt.view.HomeMenuAdapter;
import com.sopt.view.HomeMenuEditAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.LocalActivityManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class SubGroup extends Activity{
	private ListView ListView01;
	private TextView headerText;
	private FriendsGroupManager manager;
	private ArrayList<FriendsBasicMember> menuList;
	LocalActivityManager activityManager;
	private String curGroupId;
	protected boolean isEditMode = false;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sub_group);
		
		
		Button backBtn = (Button) findViewById(R.id.backButton);
		backBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		
		Button editBtn = (Button) findViewById(R.id.editButton);
		editBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if(!isEditMode ){
					subGroupActivityChangeToEdit();
				}else{
					initilizeHelpers();
				}
			}
		});
		
		headerText = (TextView) findViewById(R.id.headerText);
		curGroupId = getIntent().getAction();

		headerText.setText(getIntent().getExtras().getString("head")+"("+getIntent().getExtras().getInt("size")+")");
		
	}
	
	protected void subGroupActivityChangeToEdit() {
		isEditMode = true;
		
		showToastLong(getResources().getString(R.string.explainGroupEditMode));
		
		ListView01 = (ListView) findViewById(R.id.listFirst);
		
		GroupMemberEditAdapter infomenuAdapter = new GroupMemberEditAdapter(this,R.layout.row_member_layout, menuList);
		
		ListView01.setAdapter(infomenuAdapter);
		
		ListView01.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				FriendsBasicMember curItem = menuList.get(position);
				
				confirmDelete(curItem.get_id());
				return false;
			}
		});
		
		ListView01.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				FriendsBasicMember curItem = menuList.get(position);
					editMember(curItem.get_id());
			}
		});
	}
	
	
	protected void confirmDelete(final String getId) {
		AlertDialog.Builder ad = new AlertDialog.Builder(this);
		ad.setTitle(getResources().getString(R.string.warningdelete));
		ad.setPositiveButton("OK",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				deleteMember(getId);
			}
		});
		ad.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		ad.create().show();
		
	}

	protected void deleteMember(String getId) {
		showToastLong(this.getResources().getString(R.string.explainMemberDeleteTemp));
		
//		FriendsDBHelper helper = new FriendsDBHelper(this);
//		SQLiteDatabase db = helper.getWritableDatabase();
//		
//		String where = FriendsDB.MEMBER_ID + " = ('" + getId +"')";
//		int rs = db.delete(FriendsDB.TABLE_MEMBER, where, null);
//		
//		
//		if(rs<1){
//			showToast(this.getResources().getString(R.string.toastError));
//		}else if(rs==1){
//			showToastLong(this.getResources().getString(R.string.toastSaveComplete)
//					+"\n"
//					+this.getResources().getString(R.string.explainMemberDelete));
//			
//		}
//		db.close();
		
		initilizeHelpers();
	}

	protected void editMember(String getId) {
		Intent i = new Intent(this, SubMemberInfo.class).setAction(getId);
		startActivity(i);
	}

	private void initilizeHelpers() {
		manager = new FriendsGroupManager(this,getContentResolver());
		
		menuList = manager.getMemberList(curGroupId);
		
		ListView01 = (ListView) findViewById(R.id.listFirst);
		
		GroupMemberAdapter infomenuAdapter = new GroupMemberAdapter(this,R.layout.row_member_layout, menuList);

		ListView01.setAdapter(infomenuAdapter);

		ListView01.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				final TextView numberView = (TextView) view.findViewById(R.id.homeMemberRowNumber);
				
				AlertDialog listdialog = new AlertDialog.Builder(
						SubGroup.this).setTitle(
						"To "+numberView.getText()).setItems(R.array.member_menu,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								switch (which) {
								case 0:
									Intent i = new Intent(SubGroup.this, FriendsSms.class).setAction(numberView.getText().toString());
									startActivity(i);
									break;
								case 1:
									startActivity(new Intent(
											Intent.ACTION_CALL, Uri
													.parse("tel:"+ numberView.getText())));
									break;
								case 2:
									Intent i2 = new Intent(SubGroup.this, ChatActivity.class).setAction(numberView.getText().toString());
									startActivity(i2);
									break;
								}
							}
						}).create();
				listdialog.show();
			}			
		});
		
		isEditMode = false;
	}
	
	protected void onResume() {
		super.onResume();
		initilizeHelpers();		
	}
	
	private void showToast(String msg){
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}
	private void showToastLong(String msg){
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}
	
}
